package nkr1pt.popkorn.interact;

import com.google.inject.Inject;
import java.util.Map;
import nkr1pt.popkorn.download.DownloadCandidate;

/**
 *
 * @author Kristof Vanhaeren
 */
public class ConsoleStatusView {

    private PopkornStatusBuilder popkornStatusBuilder;
    
    @Inject
    public ConsoleStatusView(PopkornStatusBuilder popkornStatusBuilder) {
        this.popkornStatusBuilder = popkornStatusBuilder;
    }

    public String getView() {
        PopkornStatus status = popkornStatusBuilder.buildStatus();
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("\n");
        stringBuilder.append("-------------------------------------");
        stringBuilder.append("\n");
        stringBuilder.append("Popkorn status:");
        stringBuilder.append("\n");
        stringBuilder.append("-------------------------------------");
        stringBuilder.append("\n");
        stringBuilder.append("\n");

        stringBuilder.append("Scheduler mode: " + status.getSchedulerMode().toString());

        stringBuilder.append("\n");
        stringBuilder.append("\n");

        stringBuilder.append("Active downloads (" + status.getBusyDownloads().size() + "/" + status.getTotalDownloads() + ")");
        stringBuilder.append("\n");
        for (Map.Entry<DownloadCandidate, Integer> entry : status.getBusyDownloads().entrySet()) {
            stringBuilder.append(entry.getKey().getName() + " - " + entry.getValue() + "%");
            stringBuilder.append("\n");
        }

        stringBuilder.append("\n");

        stringBuilder.append("Completed downloads (" + status.getCompletedDownloads().size() + "/" + status.getTotalDownloads() + ")");
        for (DownloadCandidate candidate : status.getCompletedDownloads()) {
            stringBuilder.append(candidate.getName());
        }

        stringBuilder.append("\n");

        stringBuilder.append("Downloads in queue (" + status.getWaitingDownloads().size() + "/" + status.getTotalDownloads() + ")");
        for (DownloadCandidate candidate : status.getWaitingDownloads()) {
            stringBuilder.append(candidate.getName());
        }

        stringBuilder.append("\n");

        stringBuilder.append("Failed downloads (" + status.getFailedDownloads().size() + "/" + status.getTotalDownloads() + ")");
        for (DownloadCandidate candidate : status.getFailedDownloads()) {
            stringBuilder.append(candidate.getName());
        }

        stringBuilder.append("\n");
        stringBuilder.append("\n");

        stringBuilder.append("Extracting downloads (" + status.getExtractingDownloads().size() + "/" + status.getTotalDownloads() + ")");
        for (DownloadCandidate candidate : status.getExtractingDownloads()) {
            stringBuilder.append(candidate.getName());
        }

        stringBuilder.append("\n");

        stringBuilder.append("Extracted downloads (" + status.getExtractedDownloads().size() + "/" + status.getTotalDownloads() + ")");
        for (DownloadCandidate candidate : status.getExtractedDownloads()) {
            stringBuilder.append(candidate.getName());
        }

        stringBuilder.append("\n");
        stringBuilder.append("\n");

        return stringBuilder.toString();
    }

}